package com.kevis.zhunblog.service.impl;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kevis.zhunblog.service.BaseCommonService;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class BaseCommonServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseCommonService<T> {
    public String CacheName;

    public BaseCommonServiceImpl() {
        // getClass() 获得该类的类类型(即类型变量)
        Class clazz = getClass();
        // getGenericSuperclass() 获得该类带有泛型的父类
        Type type = clazz.getGenericSuperclass();
        // ParameterizedType 参数化类型，即泛型
        // 将Type转化为参数化类型(即泛型)
        ParameterizedType p = (ParameterizedType) type;

        // getActualTypeArguments() 获取参数化类型的数组，泛型可能有多个
        Type[] actualTypeArguments = p.getActualTypeArguments();

        if (actualTypeArguments != null && actualTypeArguments.length > 1) {
            // 将Type转化为类型变量(即Class)
            Class<T> c1 = (Class<T>) actualTypeArguments[0];
            Class<T> c2 = (Class<T>) actualTypeArguments[1];
            CacheName = c2.getAnnotation(TableName.class).value();
        }

    }
}
